
$(function() {
    validateKickout();
    validateRule();
    $('.imgcode').click(function() {
        var url = ctx + "captcha/captchaImage?type=" + captchaType + "&s=" + Math.random();
        $(".imgcode").attr("src", url);
    });
});

$.validator.setDefaults({
    submitHandler: function() {
        login();
    }
});

function checkPasswordStrength(password) {
    const minLength = 8;
    const hasUpperCase = /[A-Z]/.test(password);
    const hasLowerCase = /[a-z]/.test(password);
    const hasNumbers = /\d/.test(password);
    const hasSpecialChars = /[\W_]/.test(password);


    //无序数字判断
    var numbercheck=true;
    let reg = /(?!.*(?<re1>[a-zA-Z0-9])\k<re1>{3})(?<!.*(?<re2>[a-zA-Z0-9])\k<re2>{3})/; // 不重复4位
    function checkSequentialChars(password) {
        for (let i = 0; i < password.length - 2; i++) {
            const charCode = password.charCodeAt(i);
            // 检查当前字符与后两个字符的ASCII码值是否连续递增
            if (charCode + 1 === password.charCodeAt(i + 1) &&
                charCode + 2 === password.charCodeAt(i + 2)) {
                return false; // 存在连续递增序列，返回false
            }
        }
        return true; // 未发现连续递增序列，返回true
    }

    if (reg.test(password)&&checkSequentialChars(password)) {
        console.log("you效密码")
        numbercheck=true;
    }else{
        console.log("wu效密码")
        numbercheck=false;
    }

    // 检查是否包含大小写字母、数字和特殊字符
    if (!(hasUpperCase && hasLowerCase && hasNumbers && hasSpecialChars && numbercheck)) {

        return false;
    }
    return true;
}

/* 用户管理-重置密码 */
function resetPwd() {
    var url = ctx + 'system/user/profile/resetPwd';
    $.modal.loading($("#btnSubmit").data("loading"));
    $.modal.open("重置密码", url, '770', '380');
}

function login() {
    var username = $.common.trim($("input[name='username']").val());
    var password = $.common.trim($("input[name='password']").val());
    var domain = $.common.trim($("input[name='domain']").val());
    var validateCode = $("input[name='validateCode']").val();
    var rememberMe = $("input[name='rememberme']").is(':checked');

    if(domain!=="jyzx"){
        alert('请确认域名或联系系统管理员');
        return
    }
    $.modal.loading($("#btnSubmit").data("loading"));
    $.ajax({
        type: "post",
        url: ctx + "login",
        data: {
            "username": username,
            "password": password,
            "validateCode": validateCode,
            "rememberMe": rememberMe
        },
        success: function(r) {
            if (r.code == web_status.SUCCESS) {
                if (!checkPasswordStrength(password)) {

                    alert('密码太过简单，请修改成复杂密码后再登录。');
                    location.href = ctx + 'register';
                    return
                }
                location.href = ctx + 'index';
            } else {
            	$('.imgcode').click();
            	$(".code").val("");
            	$.modal.msg(r.msg);
            }
            $.modal.closeLoading();
        }
    });
}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            username: {
                required: true
            },
            password: {
                required: true
            }
        },
        messages: {
            username: {
                required: icon + "请输入您的用户名",
            },
            password: {
                required: icon + "请输入您的密码",
            }
        }
    })
}

function validateKickout() {
    if (getParam("kickout") == 1) {
        layer.alert("<font color='red'>您已在别处登录，请您修改密码或重新登录</font>", {
            icon: 0,
            title: "系统提示"
        },
        function(index) {
            //关闭弹窗
            layer.close(index);
            if (top != self) {
                top.location = self.location;
            } else {
                var url = location.search;
                if (url) {
                    var oldUrl = window.location.href;
                    var newUrl = oldUrl.substring(0, oldUrl.indexOf('?'));
                    self.location = newUrl;
                }
            }
        });
    }
}

function getParam(paramName) {
    var reg = new RegExp("(^|&)" + paramName + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]);
    return null;
}